package cc.marcio.systems.seven.test;

import java.io.InputStream;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Message.RecipientType;

public class ReadMessage {

    public static void main(String args[]) throws Exception { 

        String host = "pop.gmail.com";
        String port = "995";
        String user = "emaildomarciobob@gmail.com";
        String password = "tomaty9205"; 

       // Get system properties
       Properties props = System.getProperties(); 
       props.setProperty( "mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
       props.setProperty( "mail.pop3.socketFactory.fallback", "false");
       props.setProperty( "mail.pop3.port", port);
       props.setProperty( "mail.pop3.socketFactory.port", port);
       props.put("mail.pop3.host", host);

	    // Setup authentication, get session
	    Authenticator auth = new SimpleAuthenticator(user, password);
	    Session session = Session.getInstance(props,auth);
       
        // Get a Store object that implements the specified protocol.
        Store store = session.getStore("pop3");

        //Connect to the current host using the specified username and password.
        store.connect(host, 995, user, password);

        //Create a Folder object corresponding to the given name.
        Folder folder = store.getFolder("inbox");

        // Open the Folder.
        folder.open(Folder.READ_ONLY);

        Message[] message = folder.getMessages();

        // Display message.
        for (int i = 0; i < message.length; i++) {
            System.out.println("SentDate : " + message[i].getSentDate());
            
            for( int j=0; j < message[i].getAllRecipients().length; j++ )
            	System.out.println(message[i].getAllRecipients()[j]);
            
            System.out.println("From : " + message[i].getFrom()[0]);
            System.out.println("Subject : " + message[i].getSubject());
            System.out.print("Message : ");

            InputStream stream = message[i].getInputStream();
            while (stream.available() != 0) {
                System.out.print((char) stream.read());
            }
            System.out.println();
        }

        folder.close(false);
        store.close();
    }    
}
